package tgc.rj.garbage.utlis;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.crypto.interfaces.PBEKey;

import android_serialport_api.SerialPort;
import tgc.rj.garbage.custom.IConstant;

public class SerialportCkUtils extends Thread {
    public SerialPort serialPort = null;
    public InputStream inputStream = null;
    public OutputStream outputStream = null;
    public static boolean flag = false;
    private Handler handler;

    public SerialportCkUtils(Handler handler) {
        this.handler = handler;
    }

//    public SerialportCkUtils(){
//        super();
//    }

    @Override
    public void run() {
        try{
            Thread.sleep(50);
        }catch (Exception e){
            e.printStackTrace();
        }

        byte[] readData = new byte[1024];
        byte[] x = new byte[5];
        int idx = 0;
        while (flag) {
            if (inputStream == null) {
                return;
            }
            try {
                int size = inputStream.read(readData);
                if (readData[0] == (byte) 0xfa) {
                    x = new byte[5];
                    idx = 0;
                }
                if (idx + size > 5) {
                    x = new byte[5];
                    idx = 0;
                    continue;
                } else {
                    for (int i = 0; i < size; i++) {
                        x[idx++] = readData[i];
                    }
                }
                if (idx == 5) {
                    String str = bytesToHexString(x);
                    str = str.substring(0, str.length() - 1);
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putString("strS", str);
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
            stringBuilder.append(" ");
        }
        return stringBuilder.toString();
    }


    public void openSrialPort() {
        try {
            serialPort = new SerialPort(new File("/dev/" + IConstant.PORT), IConstant.BAUDRATE, 0);
            inputStream = serialPort.getInputStream();
            outputStream = serialPort.getOutputStream();
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void closeSerialPort() {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            flag = false;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /***
     * 发送串口数据
     * @param data
     */
    public void sendSerialPort(byte[] data) {
        try {
            outputStream.write(data);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
